home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / ust1a.arc / WHICH.C < prev   
Encoding:
C/C++ Source or Header  |  1985-09-05  |  3.7 KB  |  185 lines

  1. /*+
  2.  * File: which.c
  3.  * Description:
  4.  *  Tries to mimic Unix's which as closely as possible.
  5.  *  
  6.  * Usages:
  7.  *  which <i_files>
  8.  * 
  9.  *
  10.  *  Author: Mohsen Banan.
  11.  *
  12.  *  This program is public domain software, no warranty intended or
  13.  *  implied.
  14.  *  General permission to copy or modify, but not for profit,  is
  15.  *  hereby  granted.
  16.  *
  17.  * Functions:
  18.  *
  19.  *
  20.  * Audit Trail:  $Log:    which.c,v $
  21.  * Revision 1.1  85/08/28  08:38:54  mohsen
  22.  * original posting to the net
  23.  * 
  24.  * 
  25.  *
  26. -*/
  27.  
  28. #ifdef RCS_VER
  29. static char *rcs = "$Header: which.c,v 1.1 85/08/28 08:38:54 mohsen Exp $";
  30. #endif
  31.  
  32. /* #includes */
  33. #include "mbstd.h"
  34. #include <stdio.h>
  35.  
  36. /* #defines */
  37. #ifdef BSD4_2
  38. #define strchr index
  39. #endif
  40.  
  41. /* external variables */
  42.  
  43. /* referenced external function declarations */
  44. EXTERN char *getenv();
  45. EXTERN char *strchr();
  46. EXTERN char *strcat();
  47.  
  48. /* internal function declarations */
  49. PUBLIC VOID which();
  50. STATIC CHAR * get_head();
  51. STATIC BOOL is_runable();
  52. STATIC VOID usage();
  53.  
  54. /* global variables */
  55.  
  56. /* static variables */
  57. STATIC CHAR * prog_name;
  58.  
  59. INT
  60. main (argc, argv)
  61. INT argc;
  62. CHAR * argv[];
  63. {
  64.     which(argc, argv);
  65. }
  66.     
  67.  
  68. /*<
  69.  * Function:which
  70.  * Description:
  71.  *
  72.  * Returns:
  73.  *  VOID
  74.  * 
  75. >*/
  76. PUBLIC VOID 
  77. which (argc,argv)
  78. INT argc;
  79. CHAR * argv[];
  80. {
  81.  
  82.     INT i;
  83.     CHAR * head, * nxt_head;        /* head concept is a-la Csh */
  84.     BOOL end_of_path;
  85.     BOOL not_in_path;
  86.     STATIC CHAR file_buf[256];
  87.     STATIC CHAR path_buf[512];
  88.     CHAR * orig_path;
  89.  
  90.     prog_name = argv[0];
  91.     if (argc < 2) {
  92.         usage();
  93.         exit(1);
  94.     }
  95.     if ((orig_path = getenv("PATH")) == NULL) {
  96.         orig_path = ".";
  97.     }
  98.     for (i=1; i<argc; ++i) {
  99.         end_of_path = FALSE;
  100.         not_in_path = TRUE;
  101.         strcpy (path_buf, orig_path);
  102.         nxt_head = head = path_buf;
  103.         while ( ! end_of_path ) {
  104.             nxt_head = get_head (head);
  105.             if ( nxt_head == NULL) {
  106.                 end_of_path = TRUE;
  107.             } else {
  108.                 *nxt_head = '\0';
  109.             }
  110. #ifdef unix
  111.             sprintf (file_buf, "%s/%s",(*head ? head:"."), argv[i]);
  112. #endif
  113. #ifdef MSDOS
  114.             sprintf (file_buf, "%s\\%s",(*head ? head:"."), argv[i]);
  115. #endif
  116.             head = ++nxt_head;
  117.             if ( is_runable(file_buf) ) {
  118.                 printf ("%s\n", file_buf);
  119.                 not_in_path = FALSE;
  120.             }
  121.         }
  122.         if ( not_in_path ) {
  123.             printf ("No %s in %s\n", argv[i], orig_path);
  124.         }
  125.     } /* for i<argc */
  126.     exit (0);
  127. }
  128.  
  129. STATIC VOID
  130. usage ()
  131. {
  132.     fprintf (stderr, "Usage: %s cmd [cmd, ..]\n", prog_name);
  133. }
  134.  
  135.  
  136. STATIC CHAR * 
  137. get_head (head)
  138. CHAR * head;
  139. {
  140. #ifdef unix
  141.     return (strchr( head, ':'));
  142. #endif
  143. #ifdef MSDOS
  144.     return (strchr( head, ';'));
  145. #endif
  146. }
  147.  
  148. STATIC BOOL
  149. is_runable (file_buf)
  150. CHAR * file_buf;
  151. {
  152.     BOOL ret_val;
  153.     CHAR * name, * root_name;   /* root is as defined by Csh */
  154.  
  155.     ret_val = FALSE;
  156. #ifdef unix
  157.     if ( access(file_buf, 1) == 0 ) {
  158.         ret_val = TRUE;
  159.     }
  160. #endif
  161. #ifdef MSDOS
  162.     for (root_name=file_buf; *root_name; ++root_name);
  163.     name = strcat(file_buf, ".com");
  164.     if ( access(name, 0) == 0 ) {
  165.         ret_val = TRUE;
  166.     }
  167.     else {
  168.         *root_name = '\0';
  169.         name = strcat(file_buf, ".exe");
  170.         if ( access(name, 0) == 0 ) {
  171.             ret_val = TRUE;
  172.         }
  173.         else {
  174.             *root_name = '\0';
  175.             name = strcat(file_buf, ".bat");
  176.             if ( access(name, 0) == 0 ) {
  177.                 ret_val = TRUE;
  178.             }
  179.         }
  180.     }
  181.     printf ("\n %s \n", name);
  182. #endif
  183.     return (ret_val);
  184. }
  185.